package exampleQuestions;

public class RotateImage {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] image = new int[][] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },
				{ 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 },
				{ 21, 22, 23, 24, 25 } };

		printImage(image);

		//rotateImageBy90Degrees(image);
		
		System.out.println();

		//printImage(image);

		rotateImageBy180Degrees(image);
		
		System.out.println();

		printImage(image);
	}

	private static void rotateImageBy180Degrees(int[][] image) {
		
		int length = image.length;
		for (int i=0; i<length/2; i++ ) {
			for(int j=0;j<length;j++) {
				int tmp = image[i][j];
				image[i][j] = image[length-i-1][length-j-1];
				image[length-i-1][length-j-1] = tmp;
			}
		}
		
		if (length % 2 == 1) {
			int i = length/2;
			for(int j=0;j<length/2;j++) {
				int tmp = image[i][j];
				image[i][j] = image[length-i-1][length-j-1];
				image[length-i-1][length-j-1] = tmp;
			}
		}
		
	}

	private static void rotateImageBy90Degrees(int[][] image) {

		for (int i = 0; i < image.length / 2; i++) {
			for (int j = i; j < image.length - i - 1; j++) {
				int first = i;
				int last = image.length - i - 1;
				int offset = j - i;
				
				int tmp = image[first][j];
				image[first][j] = image[last-offset][first];
				image[last-offset][first] = image[last][last-offset];
				image[last][last-offset] = image[first+offset][last];
				image[first+offset][last] = tmp;
			}
		}

	}

	private static void printImage(int[][] image) {

		for (int i = 0; i < image.length; i++) {
			for (int j = 0; j < image[i].length; j++) {
				System.out.print(image[i][j] + " ");
			}
			System.out.println();
		}

	}

}
